package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ActiveItemAdapter;
import net.caixiaomi.info.adapter.OpenPrizeAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ActiveEntity;
import net.caixiaomi.info.model.ActiveListEntity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.OpenPrizeEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

import static android.R.id.list;

/**
 * Created by mac on 2018/9/6.
 */

public class OpenPrizeActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    @BindView(list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private OpenPrizeAdapter mAdapter;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.frag_group).setFitsSystemWindows(true);
        mContext=this;
        mRefresh.setOnRefreshListener(this);
        mListView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter=new OpenPrizeAdapter(null);
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.frag_home;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        OpenPrizeEntity entity= (OpenPrizeEntity) adapter.getData().get(position);
        switch (entity.getLotteryId()){
            case "2":
                Intent num=new Intent(mContext,OpenPrizeNumListActivity.class);
                num.putExtra("lotteryid","2");
                startActivity(num);
                break;
//            case "7":
//                Intent gd=new Intent(mContext,OpenPrizeNumListActivity.class);
//                gd.putExtra("lotteryid","7");
//                startActivity(gd);
//                break;
            case "1":
                Intent football=new Intent(mContext,OpenPrizeFootballAcitivity.class);
                football.putExtra("lotteryid","7");
                startActivity(football);
                break;
        }
    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("emptyStr", "");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<List<OpenPrizeEntity>>> dis = requestService.getDiscoveryOpenPrize(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<List<OpenPrizeEntity>>>() {

            @Override
            public void onSuccess(BaseCallModel<List<OpenPrizeEntity>> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                List<OpenPrizeEntity> list=data.data;
                List<MultiItemEntity> ll=new ArrayList<>();
                ll.addAll(list);
                mAdapter.setNewData(ll);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });
    }
}
